package com.nvn.washiodriver.services;

import java.util.Calendar;
import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.nvn.washiodriver.network.WebServiceCommunicatorListener;
import com.nvn.washiodriver.network.ws.BaseWSControl;
import com.nvn.washiodriver.network.ws.BaseWSControl.WebServiceFlag;
import com.nvn.washiodriver.network.ws.UpdateLocationWSControl;
import com.nvn.washiodriver.utils.Statics;

public class UpdateLocationService extends Service implements LocationListener, WebServiceCommunicatorListener {
	public static final int UPDATE_DURATION = 5000;
	public static final float MIN_DISTANCE = 5f;
	private long lastUpdate = 0;
	private final IBinder mBinder = new ServiceBinder();
	private LocationManager locationManager;

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	public class ServiceBinder extends Binder {
		UpdateLocationService getService() {
			return UpdateLocationService.this;
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();
		locationManager = (LocationManager) getApplicationContext()
				.getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				UPDATE_DURATION, MIN_DISTANCE, this);
		try {
			locationManager.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, UPDATE_DURATION,
					MIN_DISTANCE, this);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		Location loc = getCurrentLocation();
		if(loc != null){
			Statics.curLat = loc.getLatitude();
			Statics.curLon = loc.getLongitude();
		}
	}

	@Override
	public void onDestroy() {
		locationManager.removeUpdates(this);

		super.onDestroy();
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onLocationChanged(Location location) {
		Log.i("LOCATION", location.getLatitude() + " ," + location.getLongitude());
		
		long curTime = Calendar.getInstance().getTimeInMillis();
		if (curTime - lastUpdate >= 60000) { // 1 min
			Statics.curLat = location.getLatitude();
			Statics.curLon = location.getLongitude();
			
			new UpdateLocationWSControl(this, this,
					location.getLatitude() + "", location.getLongitude() + "")
					.execute();
		}
		lastUpdate = curTime;
	}

	public Location getCurrentLocation() {
		long tmpLast = 0;
		float tmpBest = Float.MAX_VALUE;
		Location bestResult = null;
		List<String> matchingProviders = locationManager.getAllProviders();
		for (String provider : matchingProviders) {
			Location location = locationManager.getLastKnownLocation(provider);
			if (location != null) {
				float accuracy = location.getAccuracy();
				long time = location.getTime();
				if (time > tmpLast && accuracy < tmpBest) {
					tmpLast = time;
					tmpBest = accuracy;
					bestResult = location;
				}
			}
		}
		return bestResult;
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
		// TODO Auto-generated method stub
		
	}

}
